<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
<!--      <el-form-item label="文件名称" prop="fileName">-->
<!--        <el-input-->
<!--          v-model="queryParams.fileName"-->
<!--          placeholder="请输入文件名称"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="文件大小" prop="fileSize">
        <el-input
          v-model="queryParams.fileSize"
          placeholder="请输入文件大小"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="文件状态" prop="storeType">
        <el-select  v-model="queryParams.fileState" placeholder="请选择文件状态" clearable size="small">
          <el-option
            v-for="dict in fileStateOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="文件状态-->
<!--0 已上传 1删除" prop="fileState">-->
<!--        <el-input-->
<!--          v-model="queryParams.fileState"-->
<!--          placeholder="请输入文件状态-->
<!--0 已上传 1删除"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="存储路径" prop="filePath">-->
<!--        <el-input-->
<!--          v-model="queryParams.filePath"-->
<!--          placeholder="请输入存储路径"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="访问路径" prop="url">
        <el-input
          v-model="queryParams.url"
          placeholder="请输入访问路径"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="模块名称" prop="modelName">
        <el-input
          v-model="queryParams.modelName"
          placeholder="请输入模块名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="表名" prop="tableName">
        <el-input
          v-model="queryParams.tableName"
          placeholder="请输入表名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字段名" prop="rowName">
        <el-input
          v-model="queryParams.rowName"
          placeholder="请输入字段名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="存储类型" prop="storeType">
        <el-select v-model="queryParams.storeType" placeholder="请选择存储类型" clearable size="small">
          <el-option
            v-for="dict in storeTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['file:log:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['file:log:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['file:log:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          @click="handleClean"
          v-hasPermi="['file:log:remove']"
        >清空</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['file:log:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="logList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="fileId" />
      <el-table-column label="文件名称" align="center" prop="fileName" />
      <el-table-column label="文件大小 单位KB" align="center" prop="fileSize" />
      <el-table-column label="文件状态" :formatter="fileStateFormat" align="center" prop="fileState" />

      <el-table-column label="文件存储路径" align="center" prop="filePath" />
      <el-table-column label="文件URL" align="center" prop="url" />
      <el-table-column label="模块莫名称" align="center" prop="modelName" />
      <el-table-column label="表名" align="center" prop="tableName" />
      <el-table-column label="字段名" align="center" prop="rowName" />
      <el-table-column label="存储类型"  :formatter="storeTypeFormat" align="center" prop="storeType" />
      <el-table-column label="上传时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['file:log:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改文件上传日志对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文件名称" prop="fileName">
          <el-input v-model="form.fileName" placeholder="请输入文件名称" />
        </el-form-item>
        <el-form-item label="文件大小 单位KB" prop="fileSize">
          <el-input v-model="form.fileSize" placeholder="请输入文件大小 单位KB" />
        </el-form-item>
        <el-form-item label="文件状态
0 已上传 1删除" prop="fileState">
          <el-input v-model="form.fileState" placeholder="请输入文件状态
0 已上传 1删除" />
        </el-form-item>
        <el-form-item label="文件存储路径" prop="filePath">
          <el-input v-model="form.filePath" placeholder="请输入文件存储路径" />
        </el-form-item>
        <el-form-item label="文件URL" prop="url">
          <el-input v-model="form.url" placeholder="请输入文件URL" />
        </el-form-item>
        <el-form-item label="模块莫名称" prop="modelName">
          <el-input v-model="form.modelName" placeholder="请输入模块莫名称" />
        </el-form-item>
        <el-form-item label="表名" prop="tableName">
          <el-input v-model="form.tableName" placeholder="请输入表名" />
        </el-form-item>
        <el-form-item label="图片存储URL" prop="rowName">
          <el-input v-model="form.rowName" placeholder="请输入图片存储URL" />
        </el-form-item>
        <el-form-item label="存储类型" prop="storeType">
          <el-select v-model="form.storeType" placeholder="请选择存储类型：0 BOS[百度云]">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listLog, getLog, delLog, addLog, updateLog,cleanFileUploadLog } from "@/api/file/log";

export default {
  name: "Log",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 文件上传日志表格数据
      logList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      fileStateOptions:[],
      storeTypeOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        fileName: null,
        fileSize: null,
        fileState: null,
        filePath: null,
        url: null,
        modelName: null,
        tableName: null,
        rowName: null,
        storeType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("file_state").then(response => {
      this.fileStateOptions = response.data;
    });
    this.getDicts("store_type").then(response => {
      this.storeTypeOptions = response.data;
    });
  },
  methods: {
    fileStateFormat(row, column){
      return this.selectDictLabel(this.fileStateOptions, row.fileState);
    },
    storeTypeFormat(row, column){
      return this.selectDictLabel(this.storeTypeOptions, row.storeType);
    },
    /** 查询文件上传日志列表 */
    getList() {
      this.loading = true;
      listLog(this.queryParams).then(response => {
        this.logList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        fileId: null,
        fileName: null,
        fileSize: null,
        fileState: null,
        filePath: null,
        url: null,
        modelName: null,
        tableName: null,
        rowName: null,
        storeType: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.fileId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加文件上传日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const fileId = row.fileId || this.ids
      getLog(fileId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改文件上传日志";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.fileId != null) {
            updateLog(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addLog(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const fileIds = row.fileId || this.ids;
      this.$confirm('是否确认删除文件上传日志编号为"' + fileIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delLog(fileIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 清空按钮操作 */
    handleClean() {
      this.$confirm('是否确认清空所有文件日志数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function() {
        return cleanFileUploadLog();
      }).then(() => {
        this.getList();
        this.msgSuccess("清空成功");
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('file/log/export', {
        ...this.queryParams
      }, `file_log.xlsx`)
    }
  }
};
</script>
